/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sge.Datos;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author vivancu
 */
public class ModeloTabla extends AbstractTableModel {

    private static final Logger LOG = Logger.getLogger(ModeloTabla.class.getName());
    private String sql;
    private Statement stmt;
    private ResultSet rs;
    private ResultSetMetaData rsmt;
    private int rows = -1;
    private Class[] colClasses;
    private ArrayList colNames;
    private boolean canEdit;
    public int[] pks = null;

    //private List colClasses = null;
    public ModeloTabla(String sql, ArrayList columnas, boolean editable) {
        try {
            this.stmt = FabricaConexion.getInstancia().crearSentencia();
            this.sql = sql;
            this.canEdit = editable;
            this.colNames = columnas;

            if (colNames == null) {
                int colCount = 0;
                colCount = rsmt.getColumnCount();
                this.colNames = new ArrayList();
                for (int i = 1; i <= colCount; i++) {
                    String colLabel = rsmt.getColumnLabel(i);
                    this.colNames.add(colLabel);
                }
            }

            recargarDatos();

        } catch (SQLException ex) {
            LOG.log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public String getColumnName(int column) {
        return (String) colNames.get(column);
    }

    @Override
    public int getRowCount() {
        if (rows == -1) {
            try {
                rs.last();
                rows = rs.getRow();
            } catch (SQLException ex) {
                LOG.log(
                        Level.SEVERE, null, ex);
            }
        }
        return rows;
    }

    @Override
    public int getColumnCount() {
        return colNames.size();
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        Object retorno = null;
        try {
            rs.absolute(rowIndex + 1);
            retorno = rs.getObject(columnIndex + 1);

        } catch (SQLException ex) {
            LOG.log(
                    Level.SEVERE, null, ex);
        }
        return retorno;
    }

    @Override
    public Class getColumnClass(int columnIndex) {
        if (colClasses == null) {
            colClasses = new Class[colNames.size()];
            try {
                for (int i = 0; i < colNames.size(); i++) {
                    try {
                        String className = rsmt.getColumnClassName(i + 1);
                        Class c = Class.forName(className);
                        colClasses[i] = c;
                    } catch (ClassNotFoundException ex) {
                        LOG.log(
                                Level.SEVERE, null, ex);
                    }
                }
            } catch (SQLException ex) {
                LOG.log(
                        Level.SEVERE, null, ex);
            }
        }
        Class c = colClasses[columnIndex];
        return c;

    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return canEdit;
    }

    public Object[] getPrimaryKeys(int rowIndex) {
        Object[] retorno = null;
        try {
            if (pks == null) {
                pks = new int[]{0};
            }
            retorno = new Object[pks.length];
            rs.absolute(rowIndex + 1);
            for (int i = 0; i < pks.length; i++) {
                retorno[i] = rs.getObject(pks[i] + 1);
            }
        } catch (SQLException ex) {
            LOG.log(
                    Level.SEVERE, null, ex);
        }
        return retorno;
    }

    public void recargarDatos() {
        try {

            rs = stmt.executeQuery(this.sql);
            rsmt = rs.getMetaData();

        } catch (SQLException ex) {
            LOG.log(Level.SEVERE, null, ex);
        }

    }

    public String getSql() {
        return sql;
    }

    public void setSql(String sql) {
        this.sql = sql;
    }
}
